home *** CD-ROM | disk | FTP | other *** search
/ Celestin Apprentice 7 / Apprentice-Release7.iso / Source Code / C / Applications / Moscow ML 1.42 / MSLpatches / ->MSL C.sit / ->MSL C / ->MSL Mac / into Source / stat.mac.c / stat.mac.c
Encoding:
C/C++ Source or Header  |  1997-05-21  |  3.6 KB  |  163 lines  |  [TEXT/CWIE]

  1. /*  Metrowerks Standard Library  Version 2.1.2b7  1997 May  */
  2.  
  3. /*
  4.  *    File:        stat.c
  5.  *                ©1993-1997 metrowerks Inc. All rights reserved
  6.  *    Author:        Berardino E. Baratta
  7.  *
  8.  *    Content:    Interface file to standard UNIX-style entry points ...
  9.  *
  10.  *    NB:            This file implements some UNIX low level support.  These functions
  11.  *                are not guaranteed to be 100% conformant.
  12.  *
  13.  */
  14.  
  15. #include <stat.h>
  16.  
  17. #include <errno.h>
  18. #include <unistd.h>
  19.  
  20. #include <Files.h>
  21. #include <LowMem.h>
  22. #include <Types.h>
  23. #include <time.mac.h>                  /*mm970514 */
  24.  
  25. /* function prototypes for externally defined functions */
  26. #ifdef __cplusplus
  27. extern "C" {
  28. #endif
  29.  
  30. extern int __ctopstring(const char *cstring, Str255 pstring);
  31.  
  32. #ifdef __cplusplus
  33. }
  34. #endif
  35.  
  36. /*
  37.  *    static int __stat(short vrefnum, long dirid, Str255 pname, struct stat *buf)
  38.  *
  39.  *        Returns information about a file (called by fstat and stat).
  40.  */
  41. static int __stat(short vrefnum, long dirid, Str255 pname, struct stat *buf)
  42. {
  43.     HFileInfo        fpb;
  44.     HVolumeParam    vpb;
  45.     OSErr            err;
  46.     Str255            name;
  47.  
  48.     fpb.ioNamePtr = pname;
  49.     fpb.ioFDirIndex = 0;
  50.     fpb.ioVRefNum = vrefnum;
  51.     fpb.ioDirID = dirid;
  52.  
  53.     /* get the file's catalog info */
  54.     err = PBGetCatInfoSync((CInfoPBPtr)&fpb);
  55.     if (err == noErr) {
  56.         /* get the volume's info */
  57.         vpb.ioVolIndex = 0;
  58.         vpb.ioNamePtr = name;
  59.         vpb.ioVRefNum = vrefnum;
  60.         err = PBHGetVInfoSync((HParmBlkPtr)&vpb);
  61.         if (err == noErr && buf != NULL) {
  62.             /* fill in the data */
  63.             if (fpb.ioFlAttrib & 0x10)
  64.             {
  65.                 buf->st_mode = S_IFDIR;
  66.                 buf->st_nlink = 2;
  67.             }                
  68.             else
  69.             {
  70.                 buf->st_nlink = 1;
  71.                 if (fpb.ioFlFndrInfo.fdFlags & 0x8000)
  72.                     buf->st_mode = S_IFLNK;
  73.                 else
  74.                     buf->st_mode = S_IFREG;
  75.             }
  76.             buf->st_ino = fpb.ioDirID;
  77.             buf->st_dev = fpb.ioVRefNum;
  78.             buf->st_uid = getuid();
  79.             buf->st_gid = getgid();
  80.             buf->st_rdev = 0;
  81.             buf->st_size = fpb.ioFlLgLen;
  82.             buf->st_atime = buf->st_mtime = fpb.ioFlMdDat + _mac_unix_epoch_offset_;       /*mm 970514*/
  83.             buf->st_ctime = fpb.ioFlCrDat + _mac_unix_epoch_offset_;                       /*mm 970514*/
  84.             buf->st_blksize = vpb.ioVAlBlkSiz;
  85.             buf->st_blocks = (buf->st_size + buf->st_blksize - 1) / buf->st_blksize;
  86.         }
  87.     }
  88.  
  89.     if (err != noErr)
  90.         errno = err;
  91.     return (err == noErr ? 0 : -1);
  92. }
  93.  
  94. /*
  95.  *    int stat(char *path, struct stat *buf)
  96.  *
  97.  *        Returns information about a file.
  98.  */
  99. int stat(const char *path, struct stat *buf)
  100. {
  101.     Str255            ppath;
  102.  
  103.     if (path) {
  104.         /* convert the C string into a Pascal string */
  105.         if (__ctopstring(path, ppath) != noErr) return (-1);
  106.     
  107.         return (__stat(0, 0L, ppath, buf));
  108.     }
  109.     return (-1);
  110. }
  111.  
  112. /*
  113.  *    int fstat(int fildes, struct stat *buf)
  114.  *
  115.  *        Returns information about a file.
  116.  */
  117. int fstat(int fildes, struct stat *buf)
  118. {
  119.     Str255            fname;
  120.     FCBPBRec        fcbpb;
  121.     OSErr            err;
  122.  
  123.     fcbpb.ioFCBIndx = 0;
  124.     fcbpb.ioRefNum = fildes;
  125.     fcbpb.ioNamePtr = (StringPtr)fname;
  126.  
  127.     err = PBGetFCBInfoSync(&fcbpb);
  128.  
  129.     if (err != noErr)
  130.         errno = err;
  131.     return (err == noErr ? __stat(fcbpb.ioFCBVRefNum, fcbpb.ioFCBParID, fcbpb.ioNamePtr, buf) : -1);
  132. }
  133.  
  134. /*
  135.  *    int mkdir(const char *path, int mode)
  136.  *
  137.  *        Creates a directory. (NB: mode is ignored on the mac)
  138.  */
  139. int mkdir(const char *path, int mode)
  140. {
  141.     HFileParam        fpb;
  142.     Str255            ppath;
  143.     OSErr            err = -1;
  144.  
  145.     if (path) {
  146.         /* convert the c string into a pascal string */
  147.         if (__ctopstring(path, ppath) != noErr) return (-1);
  148.  
  149.         fpb.ioNamePtr = ppath;
  150.         fpb.ioVRefNum = 0;
  151.         fpb.ioDirID = 0L;
  152.         err = PBDirCreateSync((HParmBlkPtr)&fpb);
  153.     }
  154.  
  155.     if (err != noErr)
  156.         errno = err;
  157.     return (err == noErr ? 0 : -1);
  158. }
  159.  
  160. /*  Change Record
  161.  *    30-Dec-95  JFH  Removed uses of OLDROUTINENAMES
  162.  *  mm 970514       Added correction for difference in 1900Jan01 and 1904Jan01 epochs
  163. */